package com.cxg.fairy.ec.sign;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatImageButton;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cxg.fairy.delegates.LatteDelegate;
import com.cxg.fairy.net.RestClient;
import com.cxg.fairy.net.callback.IError;
import com.cxg.fairy.net.callback.ISuccess;
import com.cxg.fairy.ui.widget.CheckBox.CheckBoxGType;
import com.cxg.fairy.util.checkcode.CodeUtils;
import com.cxg.fairy.util.checkcode.PhoneFormatCheckUtils;
import com.cxg.fairy.util.log.LatteLogger;
import com.cxg.fairy.util.toasty.Toasty;
import com.diabin.latte.ec.R;
import com.diabin.latte.ec.R2;
import com.rengwuxian.materialedittext.MaterialEditText;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * Created by CFairy on 2017/11/22
 */

public class SignUpDelegate extends LatteDelegate {

    String getCode = null;

    @OnClick(R2.id.back_home)
    void onClickBackHome() {
        getSupportDelegate().pop();
    }

    @BindView(R2.id.checkBoxG)
    CheckBoxGType myCheckBox = null;

    @BindView(R2.id.met_reg_tel)
    MaterialEditText metRegTel = null;

    @BindView(R2.id.met_reg_checkcode)
    MaterialEditText metRegCheckCode = null;

    @BindView(R2.id.aib_reg_checkcode_img)
    AppCompatImageButton ibRegCheckCodeImg = null;

    @BindView(R2.id.acb_reg_btn)
    AppCompatButton btnReg = null;

    @OnClick(R2.id.aib_reg_checkcode_img)
    void onClickCheckCodeImg() {
        ibRegCheckCodeImg.setImageBitmap(CodeUtils.getInstance().getBitmap());
    }

    private ISignListener mISignListener = null;

    @SuppressWarnings("deprecation")
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof ISignListener) {
            mISignListener = (ISignListener) activity;
        }
    }

    @OnClick(R2.id.acb_reg_btn)
    void onClickSignUp() {
        if (checkForm()) {
            final String tel = metRegTel.getText().toString();
            RestClient.builder()
                    .url("getCode")
                    .params("phoneNum",tel)
                    .success(new ISuccess() {
                        @Override
                        public void onSuccess(String response) {
                            int code = JSON.parseObject(response).getInteger("code");
                            String msg = JSON.parseObject(response).getString("message");
                            if (code == 1) {
                                final JSONObject obj = JSON.parseObject(response).getJSONObject("data");
                                String verifyCode = obj.getString("verifyCode");
                                getSupportDelegate().start(SignUpByNoDelegate.create(tel, verifyCode));
                            } else {
                                Toasty.error(getContext(), msg).show();
                            }

                        }
                    })
                    .error(new IError() {
                        @Override
                        public void onError(int code, String msg) {
                        }
                    })
                    .build()
                    .post();
        }
    }

    void onClickLink() {
        getSupportDelegate().start(new SignInDelegate());
    }

    private boolean checkForm() {
        boolean isPass = false;
        String phone = metRegTel.getText().toString();
        String checkCode = metRegCheckCode.getText().toString();

        if (!PhoneFormatCheckUtils.isPhoneLegal(phone)) {
            isPass = false;
        } else {
            if (checkPicCode()) {
                isPass = true;
            } else {
                isPass = false;
            }
        }
        return isPass;
    }



    @Override
    public Object setLayout() {
        return R.layout.delegate_sign_up;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        myCheckBox.setChecked(true);
        ibRegCheckCodeImg.setImageBitmap(CodeUtils.getInstance().getBitmap());

        //图片验证码输入框 监听
        metRegCheckCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
            @Override
            public void afterTextChanged(Editable s) {
                if (checkPicCode()) {
                    btnReg.setEnabled(true);
                    btnReg.setBackgroundColor(Color.parseColor("#018b18"));
                } else {
                    btnReg.setEnabled(false);
                    btnReg.setBackgroundColor(Color.parseColor("#9c9c9c"));
                }
            }
        });

        //手机号码输入框 监听
        metRegTel.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
            @Override
            public void afterTextChanged(Editable s) {
                if (checkForm()) {
                    btnReg.setEnabled(true);
                    btnReg.setBackgroundColor(Color.parseColor("#018b18"));
                } else {
                    btnReg.setEnabled(false);
                    btnReg.setBackgroundColor(Color.parseColor("#9c9c9c"));
                }
            }
        });

    }

    private boolean checkPicCode() {
        getCode = CodeUtils.getInstance().getCode();
        String picInputCode = metRegCheckCode.getText().toString();
        if (getCode.equalsIgnoreCase(picInputCode)){
            return true;
        }else{
            return false;
        }
    }

}